From 1822fb453128a1b5de93b4c590cd272d6488a077 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Fri, 25 May 2018 19:26:58 +0200
Subject: [PATCH] Add support for a ALSA_PLUGIN_DIR environment variable.

If it is not set, default to previous behavior.
---
 src/control/control.c |  6 +++++-
 src/dlmisc.c          |  9 +++++++--
 src/pcm/pcm.c         |  9 +++++++--
 src/pcm/pcm_rate.c    | 13 +++++++++----
 4 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/src/control/control.c b/src/control/control.c
index 11f7815..9dba7dd 100644
--- a/src/control/control.c
+++ b/src/control/control.c
@@ -1331,7 +1331,11 @@ static int snd_ctl_open_conf(snd_ctl_t **ctlp, const char *name,
 			build_in++;
 		}
 		if (*build_in == NULL) {
-			buf1 = malloc(strlen(str) + sizeof(ALSA_PLUGIN_DIR) + 32);
+			char* plugdir = ALSA_PLUGIN_DIR;
+			char* envplugdir = getenv("ALSA_PLUGIN_DIR");
+			if(envplugdir != NULL)
+				plugdir = envplugdir;
+			buf1 = malloc(strlen(str) + strlen(plugdir) + 32);
 			if (buf1 == NULL) {
 				err = -ENOMEM;
 				goto _err;
diff --git a/src/dlmisc.c b/src/dlmisc.c
index 3757d33..92aa864 100644
--- a/src/dlmisc.c
+++ b/src/dlmisc.c
@@ -82,9 +82,14 @@ void *snd_dlopen(const char *name, int mode, char *errbuf, size_t errbuflen)
 	char *filename = NULL;
 
 	if (name && name[0] != '/') {
-		filename = alloca(sizeof(ALSA_PLUGIN_DIR) + 1 + strlen(name) + 1);
+		char* plugdir = ALSA_PLUGIN_DIR;
+		char* envplugdir = getenv("ALSA_PLUGIN_DIR");
+		if(envplugdir != NULL)
+			plugdir = envplugdir;
+
+		filename = malloc(strlen(plugdir) + 1 + strlen(name) + 1);
 		if (filename) {
-			strcpy(filename, ALSA_PLUGIN_DIR);
+			strcpy(filename, plugdir);
 			strcat(filename, "/");
 			strcat(filename, name);
 			handle = dlopen(filename, mode);
diff --git a/src/pcm/pcm.c b/src/pcm/pcm.c
index 11aec80..590e8b6 100644
--- a/src/pcm/pcm.c
+++ b/src/pcm/pcm.c
@@ -2496,13 +2496,18 @@ static int snd_pcm_open_conf(snd_pcm_t **pcmp, const char *name,
 			build_in++;
 		}
 		if (*build_in == NULL) {
-			buf1 = malloc(strlen(str) + sizeof(ALSA_PLUGIN_DIR) + 32);
+			char* plugdir = ALSA_PLUGIN_DIR;
+			char* envplugdir = getenv("ALSA_PLUGIN_DIR");
+			if(envplugdir != NULL)
+				plugdir = envplugdir;
+			buf1 = malloc(strlen(str) + strlen(plugdir) + 32);
+
 			if (buf1 == NULL) {
 				err = -ENOMEM;
 				goto _err;
 			}
 			lib = buf1;
-			sprintf(buf1, "%s/libasound_module_pcm_%s.so", ALSA_PLUGIN_DIR, str);
+			sprintf(buf1, "%s/libasound_module_pcm_%s.so", plugdir, str);
 		}
 	}
 #ifndef PIC
diff --git a/src/pcm/pcm_rate.c b/src/pcm/pcm_rate.c
index 4e0c7ca..8694a38 100644
--- a/src/pcm/pcm_rate.c
+++ b/src/pcm/pcm_rate.c
@@ -1260,7 +1260,8 @@ static const char *const default_rate_plugins[] = {
 
 static int rate_open_func(snd_pcm_rate_t *rate, const char *type, const snd_config_t *converter_conf, int verbose)
 {
-	char open_name[64], open_conf_name[64], lib_name[128], *lib = NULL;
+	char open_name[64], open_conf_name[64], *lib = NULL;
+	char *buf1;
 	snd_pcm_rate_open_func_t open_func;
 	snd_pcm_rate_open_conf_func_t open_conf_func;
 	int err;
@@ -1268,9 +1269,13 @@ static int rate_open_func(snd_pcm_rate_t *rate, const char *type, const snd_conf
 	snprintf(open_name, sizeof(open_name), "_snd_pcm_rate_%s_open", type);
 	snprintf(open_conf_name, sizeof(open_conf_name), "_snd_pcm_rate_%s_open_conf", type);
 	if (!is_builtin_plugin(type)) {
-		snprintf(lib_name, sizeof(lib_name),
-				 "%s/libasound_module_rate_%s.so", ALSA_PLUGIN_DIR, type);
-		lib = lib_name;
+		char* plugdir = ALSA_PLUGIN_DIR;
+		char* envplugdir = getenv("ALSA_PLUGIN_DIR");
+		if(envplugdir != NULL)
+			plugdir = envplugdir;
+		buf1 = malloc(strlen(type) + strlen(plugdir) + 32);
+		sprintf(buf1, "%s/libasound_module_rate_%s.so", plugdir, type);
+		lib = buf1;
 	}
 
 	rate->rate_min = SND_PCM_PLUGIN_RATE_MIN;
-- 
2.17.0

